package com.hww.doc.controller;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.hww.doc.business.annotation.BussinessLog;
import com.hww.doc.business.entity.Itemsets;
import com.hww.doc.business.enums.ResponseStatus;
import com.hww.doc.business.service.DocItemsetService;
import com.hww.doc.business.vo.ItemsetsConditionVO;
import com.hww.doc.framework.object.PageResult;
import com.hww.doc.framework.object.ResponseVO;
import com.hww.doc.util.ResultUtil;

@RestController
@RequestMapping("/itemset")
public class RestItemsetController {
	@Autowired
	DocItemsetService itemsetService;
	
	@RequiresPermissions("itemsets")
	@PostMapping("/list")
	public PageResult list(ItemsetsConditionVO vo) {
		PageInfo<Itemsets> pageInfo = itemsetService.findPageBreakByCondition(vo);
		return ResultUtil.tablePage(pageInfo);
	}
	
	@RequiresPermissions("itemset:add")
    @PostMapping(value = "/add")
    @BussinessLog("添加标签")
    public ResponseVO add(Itemsets itemsets) {
		itemsets = itemsetService.insert(itemsets);
        return ResultUtil.success("标签添加成功！新标签 - " + itemsets.getItemName(), itemsets);
    }

    @RequiresPermissions(value = {"itemset:batchDelete", "itemset:delete"}, logical = Logical.OR)
    @PostMapping(value = "/remove")
    @BussinessLog("删除标签")
    public ResponseVO remove(Long[] ids) {
        if (null == ids) {
            return ResultUtil.error(500, "请至少选择一条记录");
        }
        for (Long id : ids) {
        	itemsetService.removeByPrimaryKey(id);
        }
        return ResultUtil.success("成功删除 [" + ids.length + "] 个标签");
    }

    @RequiresPermissions("itemset:get")
    @PostMapping("/get/{id}")
    @BussinessLog("获取标签详情")
    public ResponseVO get(@PathVariable Long id) {
        return ResultUtil.success(null, this.itemsetService.getByPrimaryKey(id));
    }

    @RequiresPermissions("itemset:edit")
    @PostMapping("/edit")
    @BussinessLog("编辑标签")
    public ResponseVO edit(Itemsets itemsets) {
        try {
            itemsetService.updateSelective(itemsets);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("标签修改失败！");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }
}
